<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form :model="regForm" :rules="regRules" ref="regForm">
        <!-- 用户名 -->
        <el-form-item prop="username">
          <el-input
            v-model="regForm.username"
            placeholder="请输入用户名"
            prefix-icon="el-icon-user"
          ></el-input>
        </el-form-item>
        <!-- 密码 -->
        <el-form-item prop="password">
          <el-input
            v-model="regForm.password"
            type="password"
            placeholder="请输入密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <!-- 确认密码 -->
        <el-form-item prop="repassword">
          <el-input
            v-model="regForm.repassword"
            type="password"
            placeholder="请再次确认密码"
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button style="width: 100%" type="primary" class="reg"
          @click="reg"
            >注册</el-button
          >
          <el-link type="info" @click="$router.push('./login')">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
// 表单校验步骤
// 1.定义规则
// 2.应用规则
// el-form  -> model  rules
// el-form-item  -> prop
// 3.兜底校验
export default {
  name: 'BigeventReg',

  data() {
    const validatePass2 = (rule, value, callback) => {
      if (value !== this.regForm.password) {
        callback(new Error('两次输入密码不一致!'))
      } else {
        callback()
      }
    }

    return {
      // 注册表单的数据对象
      regForm: { username: '', password: '', repassword: '' },
      // 注册表单的验证规则对象
      regRules: {
        username: [
          // 非空
          { required: true, message: '请输入用户名', trigger: 'blur' },
          {
            pattern: /^[a-zA-Z0-9]{1,10}$/,
            message: '请输入1~10位的数字或字母',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '请输入6~15位的非空字符',
            trigger: 'blur'
          }
        ],
        repassword: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          {
            pattern: /^\S{6,15}$/,
            message: '请输入6~15位的非空字符',
            trigger: 'blur'
          },
          // 自定义校验:两次输入的密码是否一致
          { validator: validatePass2, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    reg() {
      this.$refs.regForm.validate(async valid => {
        if (!valid) return
        const { data: res } = await this.$http.post('/api/reg', {
          username: this.regForm.username,
          password: this.regForm.password,
          repassword: this.regForm.repassword
        })
        // 判断 如果失败
        if (res.code !== 0) return this.$message.error(res.message)
        // 如果成功
        this.$message.success(res.message)
        // 跳转至登录页面
        this.$router.push('./login')
      })
    }
  }

}
</script>

<style lang="less" scoped>
.reg-container {
  background: url('../../assets/images/login_bg.jpg') center;
  background-size: cover;
  height: 100%;
  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    padding: 0 30px;
    .title-box {
      height: 60px;
      background: url('../../assets/images/login_title.png') center no-repeat;
    }
  }
}
</style>
